package com.bawei.property.adapter

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.bawei.property.R
import com.bawei.property.databinding.ItemCarParkBinding
import com.bawei.property.entity.CarApplyValues

/**
 * Author: Jason Zhang
 * Date: 2024/5/29
 * Desc: 车位申请列表适配器
 */
class CarApplyListAdapter constructor(private val list: MutableList<CarApplyValues>) : RecyclerView.Adapter<CarApplyListAdapter.ItemViewHolder>(){

    private var mListener: ItemParkOnClickListener? = null

    inner class ItemViewHolder(val binding: ItemCarParkBinding) : RecyclerView.ViewHolder(binding.root)

    interface ItemParkOnClickListener{
        fun onClick(v: View, position: Int)
    }

    fun setItemOnClickListener(listener: ItemParkOnClickListener){
        this.mListener = listener
    }

    override fun getItemCount(): Int = list.size

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        var binding = DataBindingUtil.inflate<ItemCarParkBinding>(LayoutInflater.from(parent.context), R.layout.item_car_park, parent, false)
        return ItemViewHolder(binding)
    }


    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        var entity = list[position]
        holder.binding.tvUserName.text = entity.parkName
        //设置不同颜色
        when(entity.parkUser){
            -1 -> {
                holder.binding.tvUserName.setTextColor(Color.BLACK)
                holder.binding.tvUserName.setBackgroundColor(Color.WHITE)
            }
            0 -> holder.binding.tvUserName.setBackgroundColor(Color.parseColor("#8fdb0a"))
            else -> holder.binding.tvUserName.setBackgroundColor(Color.parseColor("#CCCCCC"))
        }
        //设置点击事件
        holder.binding.tvUserName.setOnClickListener {
            //排除第一列的点击事件
            if(entity.parkUser != -1){
                mListener?.onClick(it, position)
            }
        }
    }


}